package com.zxy.salenet.web.web.controller;

import com.github.pagehelper.PageInfo;
import com.zxy.salenet.web.pojo.SalenetAdmins;
import com.zxy.salenet.web.service.AdminManageService;
import com.zxy.salenet.web.utils.Json;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author: zxy
 * @version: 1.0
 * @description: 管理员管理控制层
 */
 /**
 * AdminManageController.
 */
@Api(value = "超级管理员对普通管理员操作API",tags = "超级管理员对普通管理员操作API")
@RestController
@RequestMapping(path = "/admin")
public class AdminManageController {
    @Autowired
    private AdminManageService adminManageService;

    /**
     * 获取所有的管理员信息
     *
     *  @return 获取到的信息
     */
    @ApiOperation(value = "超级管理员查询所有的管理员信息",notes = "获取所有管理员的信息 JSON格式 {\"adminRealName\":\"\",\"pageNum\":1,\"pageSize\":10}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping("/queryAllAdmin")
    private Json queryAllAdmin(@RequestBody Map<String,String> map) {
        Json json = new Json();
        String adminRealName = map.get("adminRealName");
        Integer pageNum = Integer.parseInt(map.get("pageNum"));
        Integer pageSize =Integer.parseInt(map.get("pageSize"));
        PageInfo<SalenetAdmins> adminsPageInfo = adminManageService.queryAllAdminsMessage(adminRealName,pageNum,pageSize);
        json.setCode(200);
        json.setData(adminsPageInfo);
        json.setMessage("查询成功");
        return json;
    }


    /**
     * 添加管理员
     * @param admins
     * @return
     */
    @ApiOperation(value = "超级管理员添加一个管理员信息",notes = "管理员信息格式  JSON {\n" +
            "    \"adminsName\":\"wangfugui\",\n" +
            "    \"adminsPasswd\":\"password\",\n" +
            "    \"adminsRealname\":\"王富贵\",\n" +
            "    \"adminsPhone\":\"19876885722\",\n" +
            "    \"adminsPower\":1} ")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping("/addAdmin")
    private Json addAdmin(@RequestBody SalenetAdmins admins){
        Json json = new Json();
        Integer addResult = adminManageService.addAdmin(admins);
        if(addResult == 2){
            //如果存在重复数据
            json.setMessage("存在重复数据,请检查后重新添加");
            json.setData(2);
            json.setCode(900);
            return json;
        }
        if(addResult == 0){
            //如果添加失败
            json.setMessage("系统错误,请稍后再添加");
            json.setData(0);
            json.setCode(800);
            return json;
        }
        json.setCode(200);
        json.setMessage("添加成功");
        json.setData(1);
        return json;
    }

    /**
     * 修改管理员
     * @param admins
     * @return
     */
    @ApiOperation(value = "超级管理员修改管理员的信息",notes = "修改管理员信息  发送整条管理员对象信息 JSON{\n" +
            "    \"id\":3,\n" +
            "    \"adminsName\":\"fulei123\",\n" +
            "    \"adminsPasswd\":\"password\",\n" +
            "    \"adminsRealname\":\"付磊\",\n" +
            "    \"adminsPhone\":\"18768996755\",\n" +
            "    \"adminsPower\":0}")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(value = "modifyAdmin")
    private Json modifyAdmin(@RequestBody SalenetAdmins admins){
        Json json = new Json();
        Integer reuslt = adminManageService.updateAdmin(admins);
        if(reuslt == 0){
            json.setData(0);
            json.setMessage("修改失败");
            json.setCode(200);
            return json;
        }
        json.setData(1);
        json.setMessage("修改成功");
        json.setCode(200);
        return json;
    }

    /**
     * 根据id删除管理员
     *
     * @return 获取到的数据
     */
    @ApiOperation(value = "超级管理员删除普通管理员",notes = "发送对应的管理员数据中的 id JSON格式 {\"id\":3} ")
    @ApiImplicitParam(name="Json",value = "",dataType = "Json",required = true)
    @PostMapping(path = "/deleteAdmin")
    private Json deleteAdmin(@RequestBody Map<String,Integer> map){
        Json json = new Json();
        Integer id = map.get("id");
        Integer result = adminManageService.deleteAdmin(id);
        if(result == 0){
            json.setCode(800);
            json.setData(0);
            json.setMessage("删除失败");
            return json;
        }
        json.setCode(200);
        json.setData(1);
        json.setMessage("删除成功");
        return json;
    }
}
